﻿using System;
using System.ComponentModel;
using System.Reflection;

namespace Relay.Core.Utility {
    /// <summary>
    /// A utility class contain text related functionality.
    /// </summary>
    public static class TextUtility {
        public static string GetEnumDescriptionAttribute<T>(T value) {
            if (!(value is Enum))
                throw new ArgumentException("'value' must be an enum value.");

            MemberInfo[] enums = typeof(T).GetMember(Enum.GetName(typeof(T), value));

            // Reflect the Description of the enum value from the type
            string message = String.Empty;
            if (enums != null && enums.Length > 0) {
                object[] attributes = enums[0].GetCustomAttributes(typeof(DescriptionAttribute), false);

                if (attributes != null && attributes.Length > 0) {
                    DescriptionAttribute desc = attributes[0] as DescriptionAttribute;
                    message = desc.Description;
                }
            }

            return message;
        }
    }
}
